package com.mls.guardian.appst.chain;


import com.mls.guardian.config.AppSTConfig;
import com.mls.guardian.config.ProjectConfig;
import com.mls.guardian.dataobj.ModuleMessage;
import com.mls.guardian.em.ContainerRole;
import com.mls.guardian.em.ModuleType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by jl on 15/9/18.
 */
public class TaskInvoker {
    private static final Logger log = LoggerFactory.getLogger(TaskInvoker.class);
    private Excutors executors = new Excutors();
    private ProjectConfig project;
    private List<ModuleMessage> moduleList;
    private AppSTConfig config;
    private String versionTag;
    private ContainerRole role;

    public TaskInvoker(ProjectConfig project, AppSTConfig config, String versionTag) {
        this.project = project;
        this.config = config;
        this.versionTag = versionTag;
    }

    public TaskInvoker(ProjectConfig project, List<ModuleMessage> moduleList, AppSTConfig config, String versionTag) {
        this.project = project;
        this.moduleList = moduleList;
        this.config = config;
        this.versionTag = versionTag;
    }

    public void setExecutor(TaskExecutor taskExecutor) {
        executors.add(taskExecutor);
    }


    public TaskResult invoke() {
        TaskExecutor TaskExecutor = executors.next();

        try {
            if (TaskExecutor != null) {
                TaskResult result = TaskExecutor.exec(this);

                return result;
            } else {
                TaskResult result = new TaskResult();
                result.setSuc(true);
                return result;
            }
        } catch (Exception e) {
            TaskResult result = new TaskResult();
            e.printStackTrace();
            result.setSuc(false);
            result.setError("exec [" + TaskExecutor + "] make ex [" + e.getMessage() + "]");
            log.error("exec [" + TaskExecutor + "] make ex [" + e + "]");
            return result;
        }

    }


    public AppSTConfig getConfig() {
        return config;
    }

    public void setConfig(AppSTConfig config) {
        this.config = config;
    }

    public List<ModuleMessage> getModuleList() {
        return moduleList;
    }

    public void setModuleList(List<ModuleMessage> moduleList) {
        this.moduleList = moduleList;
    }

    public void addModule(String moduleName, ModuleType moduleType) {
        if (this.moduleList == null) {
            this.moduleList = new ArrayList<ModuleMessage>();
        }
        this.moduleList.add(new ModuleMessage(moduleName, moduleType));
    }

    public ProjectConfig getProject() {
        return project;
    }

    public void setProject(ProjectConfig project) {
        this.project = project;
    }

    public String getVersionTag() {
        return versionTag;
    }

    public void setVersionTag(String versionTag) {
        this.versionTag = versionTag;
    }

    public ContainerRole getRole() {
        return role;
    }

    public void setRole(ContainerRole role) {
        this.role = role;
    }
}
